package com.kobeliu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kobeliu.exceptions.GraceException;
import com.kobeliu.mapper.CompanyPhotoMapper;
import com.kobeliu.pojo.CompanyPhoto;
import com.kobeliu.pojo.bo.ModifyCompanyInfoBO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.result.ResponseStatusEnum;
import com.kobeliu.service.CompanyPhotoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
public class CompanyPhotoServiceImpl implements CompanyPhotoService {

    @Autowired
    private CompanyPhotoMapper companyPhotoMapper;

    @Transactional
    @Override
    public void insertOrModify(ModifyCompanyInfoBO infoBO) {

        if(StringUtils.isBlank(infoBO.getCompanyId()))
            GraceException.display(ResponseStatusEnum.COMPANY_INFO_UPDATED_ERROR);

        CompanyPhoto pending = new CompanyPhoto();
        pending.setCompanyId(infoBO.getCompanyId());
        pending.setPhotos(infoBO.getPhotos());

        //查看当前company有没有绑定数据
        CompanyPhoto isExist = companyPhotoMapper.selectOne(new QueryWrapper<CompanyPhoto>().eq("company_id",infoBO.getCompanyId()));
        if(isExist == null){
            //insert
            companyPhotoMapper.insert(pending);
        }else{
            //update
            companyPhotoMapper.update(pending,new QueryWrapper<CompanyPhoto>()
                    .eq("company_id",infoBO.getCompanyId()));
        }

    }

    @Override
    public String getPhotos(String companyId) {
        CompanyPhoto companyPhoto = companyPhotoMapper.selectOne(new QueryWrapper<CompanyPhoto>()
                .eq("company_id", companyId));

        if(companyPhoto!=null)
            return companyPhoto.getPhotos();

        return "";
    }
}
